#include "game.h"
#include <iostream>
//TODO: fix AI (currently it is random :|)
#include <cstdlib>
#include <ctime>

const int FPS = 60;
const int DELAY_TIME = 1000 / FPS;

int main(int argc, char **argv)
{
    //TODO: fix AI (currently it is random :|)
    srand( (unsigned)time( NULL ) );

    Uint32 frameStart, frameTime;
    
    std::cout << "game init attempt...\n";
    if (TheGame::Instance()->init("Battleships", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false))
    {
        std::cout << "game init success!\n";
        while (TheGame::Instance()->running())
        {
            frameStart = SDL_GetTicks();
            
            TheGame::Instance()->handleEvents();
            TheGame::Instance()->update();
            TheGame::Instance()->render();
            
            frameTime = SDL_GetTicks() - frameStart;
            
            if (frameTime < DELAY_TIME)
            {
                SDL_Delay(static_cast<int>(DELAY_TIME - frameTime));
            }
        }
    }
    else
    {
        std::cout << "game init failure - " << SDL_GetError() << "\n";
        return -1;
    }
    
    std::cout << "game closing...\n";
    TheGame::Instance()->clean();
    
    return 0;
}
